package com.wordpress.hybrid;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;

import com.wordpress.hybrid.util.Downloads;

import java.io.File;

public class Hybrid extends Activity {

    private static final String TAG = "Hybrid";
    private static final String INTERFACE_NAME = "native_interface";

    private static final String DEFAULT_URL = "file:///android_asset/web/hybrid-demo.html";
    WebView mWebView;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.main);

        mWebView = new WebView(this);
        mWebView.getSettings().setJavaScriptEnabled(true);

        setContentView(mWebView);

        refreshWebView(DEFAULT_URL);

    }

    private void refreshWebView(String url) {
        mWebView.loadUrl(url);
        HybridInterface bridge = new HybridInterface(this);
        mWebView.addJavascriptInterface(bridge, INTERFACE_NAME);
    }

    private void updateButtonText() {
        Log.v(TAG, "Calling javascript");
        mWebView.loadUrl("javascript:setButtonLabel(\"Test\")");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.webmenu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.refresh:
                refreshWebView(DEFAULT_URL);
                return true;
            case R.id.demo:
                updateButtonText();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public class HybridInterface {
        Context mContext;

        HybridInterface(Context c) {
            mContext = c;
        }

        @JavascriptInterface
        public void showToast(String toast) {
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
            updateHtml();
        }
    }

    public void updateHtml() {
        new Thread() {
            @Override
            public void run() {
                downloadAndExtractResource();
            }
        }.start();
    }

    public void downloadAndExtractResource() {
        // TODO: 暂时使用本地资源包测试，需改为一个网络地址
        final String resourceUrl = Environment.getExternalStorageDirectory() + "/resource.zip";
        final File download = new File(Environment.getExternalStorageDirectory()+"/hybridtest/temp/", "temp.zip");
        final File extract = new File(Environment.getExternalStorageDirectory()+"/hybridtest/web/");

        Downloads.download(download, resourceUrl, null);
        if (download.exists()) {
            if (Downloads.extractAndRecycle(download, extract)) {
                Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        refreshWebView("file:///" + extract.getAbsolutePath() + "/hybrid-demo.html");
                    }
                };
                new Handler(getMainLooper()).post(runnable);
                return;
            }
        }

    }

}
